<?php

class Query_builder_model extends CI_Model {

    function __construct() {

        parent::__construct();
    }

    function select($param=array()) {
        $ret = false;
        if ($param) {

            foreach ($param as $d => $i) {
                switch ($d) {
                    case 'select':
                        $select = $param['select'];
                        $this->db->select($select);
                        break;
                    case 'from':
                        $from = $param['from'];
                        $this->db->from($from);
                        break;
                    case 'join':
                        $join = $param['join'];
                        if (is_array($join[0])) {
                            foreach ($join as $d) {
                                $this->join($d);
                            }
                        } else {
                            $this->join($join);
                        }
                        break;
                    case 'where':
                        $this->db->where($param['where']);
                        break;
                    case 'where_in':
                        $this->db->where_in($param['where_in']['field'],$param['where_in']['keys']);
                        break;
                    case 'limit':
                        $limit = $param['limit'];
                        (is_array($limit)) ? $this->db->limit($limit[0], $limit[1]) : $this->db->limit($limit);
                        break;
                    case 'order_by':
                        $order_by = $param['order_by'];
                        if (is_array($order_by)) {
                            foreach ($order_by as $i => $v)
                                $this->db->order_by($i, $v);
                        } else {
                            $this->db->order_by($order_by);
                        }
                        break;
                    default :
                        break;
                }
            }

            $ret = $this->db->get();
            $this->db->flush_cache();
            if ($ret->num_rows == 0) {
                $ret = false;
            }
        }
        return $ret;
    }

    function join($param) {
        (isset($param[2])) ? $this->db->join($param[0], $param[1], $param[2]) : $this->db->join($param[0], $param[1]);
    }

    function get_last_id($from, $id_index_name='id', $where=false) {
        $ret = false;
        $param = array();
        $param['from'] = $from;
        $param['order_by'] = array($id_index_name => 'desc');
        $param['limit'] = 1;
        $param['select'] = $id_index_name;
        
        if ($where){
            $param['where'] = $where;
        }

        $res = $this->select($param);

        if ($res) {
            $res = $res->row_array();
            $ret = $res[$id_index_name];
        }

        return $ret;
    }

    function delete($param) {
        extract($param);
        
        if($from=='files'){
            $this->delete_file($param);
        }
        
        if (is_array($key)) {
            $this->db->where_in($field, $key);
        } else {
            $this->db->where($field, $key);
        }
        
        $this->db->delete($from);
        $this->db->flush_cache();
    }

    function delete_file($param) {
        extract($param);
        if (is_array($key)) {
            $this->db->where_in($field, $key);
        } else {
            $this->db->where($field, $key);
        }
        $this->db->from($from);
        $res = $this->db->get();
        $this->db->flush_cache();
        if ($res->num_rows > 0) {
            foreach ($res->result_array() as $v) {
                $location = DOC_ROOT . $v['location'];
                if (file_exists($location)) {
                    unlink($location);
                }
                
                if($v['type']=='picture'){
                    $thumbnail = DOC_ROOT . $v['thumbnail'];
                    if(file_exists($thumbnail)){
                        unlink($thumbnail);
                    }
                }
            }
        }
    }

    function insert($param) {
        extract($param);
    }
    
    function record_exist($param){
        extract($param);
        $ret = false;
        
        if($this->select(array('from'=>$from,'where'=>array($field=>$key),'limit'=>1))){
            $ret = true;
        } 
        
        return $ret;
    }

}